<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="产品id" prop="godFid">
        <el-input
          v-model="queryParams.godFid"
          placeholder="请输入产品id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="godFprodname">
        <el-input
          v-model="queryParams.godFprodname"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品品牌" prop="godAltervar2">
        <el-input
          v-model="queryParams.godAltervar2"
          placeholder="请输入产品品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星选分类" prop="xxgType">
        <el-cascader
            v-model="xxgTypeQuerySelected"
            :options="xxgTypeOptions"
            :props="{ 
              expandTrigger: 'hover',
              value:'dictValue',
              label:'dictLabel',
              leaf:'dictValue'
            }"
            @expand-change="handleXxgTypeQueryChange"
            @change="handleXxgTypeQuerySelect"
            ></el-cascader>
      </el-form-item>
      <el-form-item label="是否星选产品" prop="isXxg">
        <el-select v-model="queryParams.isXxg" placeholder="是否星选产品" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option label="是" value="isXx"/>
          <el-option label="否" value="nXx"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否新品" prop="xxgNew">
        <el-select v-model="queryParams.xxgNew" placeholder="是否新品" clearable size="small" @keyup.enter.native="handleQuery">
          <el-option label="全部" value=""/>
          <el-option label="是" value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleSetting"
        >设置星选产品</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleClear"
        >清除星选产品分类</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="xxgNewPop.open=true"
        >设置新品</el-button>
      </el-col> -->
    </el-row>

    <el-table v-loading="loading" border stripe :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品图片" align="center" prop="godMimgurl" min-width="120">
        <template slot-scope="scope">
          <img class="row-img" :src="scope.row.godMimgurl"/>
        </template>
      </el-table-column>
      <el-table-column label="产品id" align="center" prop="godFid"  min-width="200"/>
      <el-table-column label="产品名称" align="center" prop="godFprodname"  min-width="200"/>
      <el-table-column label="产品品牌" align="center" prop="godAltervar2"  min-width="200"/>
      
      <el-table-column label="星选分类" align="center" prop="xxgTypeName"  min-width="200"/>
      <el-table-column label="产品品类" align="center" prop="godVfDesc03"  min-width="200"/>
      <el-table-column label="产品规格" align="center" prop="godFprodstandard"  min-width="200"/>
      <el-table-column label="是否新品" align="center" prop="xxgNew"  min-width="200"/>
      <el-table-column label="操作" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleClear(scope.row)"
          >清除星选产品分类</el-button>
          <el-button
            size="mini"
            type="text"
            @click="openXxgNewPop(scope.row)"
          >设置新品</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品模型上线对话框 -->
    <el-dialog class="dialog-table" title="产品上线" :visible.sync="open" width="600px" append-to-body>
      <el-form :model="form" ref="form" :inline="true" label-width="68px">
        <el-form-item label="星选分类" prop="xxgType">
          <el-cascader
            v-model="xxgTypeSelected"
            :options="xxgTypeOptions"
            :props="{ 
              expandTrigger: 'hover',
              value:'dictValue',
              label:'dictLabel',
              leaf:'dictValue'
            }"
            style="width: 400px;"
            @expand-change="handleXxgTypeChange"
            @change="handleXxgTypeSelect"
            ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog class="dialog-table" title="新品上线" :visible.sync="xxgNewPop.open" width="600px" append-to-body>
      <el-form :model="xxgNewPop" ref="xxgNewPop" :inline="true" label-width="68px">
        <el-form-item label="是否新品" prop="xxgNew">
          <el-select v-model="xxgNewPop.value" placeholder="是否新品">
            <el-option label="是" value="1"/>
            <el-option label="否" value="0"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormNew">确 定</el-button>
        <el-button @click="xxgNewPop.open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStarProduct } from "@/api/StarProduct/StarProduct";
import requestCrm from '@/utils/requestCrm'

export default {
  name: "starProduct",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 产品模型上线表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      xxgTypeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        godFid: undefined,
        godFprodname: undefined,
        godAltervar2: '华耐星选',
        xxgTypeName: undefined,
        isXxg:undefined,
        xxgNew:undefined
      },
      xxgTypeSelected:[],
      xxgTypeQuerySelected:[],
      // 表单参数
      form: {},
      xxgNewPop:{
        value:'',
        open:false
      },
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getDicts("XXCP_TYPE").then(response => {
      response.data.forEach(i => {
        i.children = []
      });
      this.xxgTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询产品模型上线列表 */
    getList() {
      this.loading = true;
      listStarProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
        xxgType: undefined,
        xxgTypeName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.xxgTypeQuerySelected = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.godFid)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleSetting() {
      this.reset();
      this.form.ids = this.ids.join(",");
      this.form.xxgTypeSelected = [];
      this.open = true;
    },
    handleXxgTypeChange(e){
      if(e&&e.length){
        this.getDicts(e[0]).then(response => {
          let findObj = this.xxgTypeOptions.find(obj=>{
            return obj.dictValue == e[0]
          })
          if(findObj){
            findObj.children = response.data
          }
        });
      }
    },
    handleXxgTypeQueryChange(e){
      if(e&&e.length){
        this.getDicts(e[0]).then(response => {
          let findObj = this.xxgTypeOptions.find(obj=>{
            return obj.dictValue == e[0]
          })
          if(findObj){
            findObj.children = response.data
          }
        });
      }
    },
    handleXxgTypeSelect(e){
      let parent = this.xxgTypeOptions.find(obj=>{
        return obj.dictValue == e[0]
      })
      let child = ''
      if(parent){
        child = parent.children.find(obj=>{
          return obj.dictValue == e[1]
        })
      }
      if(child){
        this.form.xxgType = child.dictValue;
        this.form.xxgTypeName = this.selectDictLabel(parent.children, child.dictValue);  
      }
    },
    handleXxgTypeQuerySelect(e){
      let parent = this.xxgTypeOptions.find(obj=>{
        return obj.dictValue == e[0]
      })
      let child = ''
      if(parent){
        child = parent.children.find(obj=>{
          return obj.dictValue == e[1]
        })
      }
      if(child){
        this.queryParams.xxgType = child.dictValue;
        this.queryParams.xxgTypeName = this.selectDictLabel(parent.children, child.dictValue);  
      }
    },
    openXxgNewPop(row){
      if(!row.xxgType){
        this.msgError("请先选择星选分类");
        return
      }
      this.xxgNewPop.id = row.godFid
      this.xxgNewPop.open = true;
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.loading){
        return
      }
      if(!this.form.xxgType){
        this.msgError("请选择星选分类");
        return
      }
      this.loading = true
      requestCrm({
        url:'WxService?method=wxProUpdateXxType',
        method: 'post',
        data: this.form
      }).then(response => {
        this.loading = false
        if (response.status === '00000') {
          this.msgSuccess("新增成功");
          this.open = false;
          this.getList();  
        }else{
          this.msgSuccess(response.message);
        }
      }).catch(()=>{
        this.loading = false
      });
    },
    handleClear(row){
      this.$confirm('是否确定清除该产品的星选分类?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=>{
        if(this.loading){
          return
        }
        this.loading = true
        requestCrm({
          url:'WxService?method=wxProUpdateXxType',
          method: 'post',
          data: {ids:row.godFid, xxgType:''}
        }).then(response => {
          requestCrm({
            url:'WxService?method=wxProUpdateXxNew',
            method: 'post',
            data: {xxgNew:'0', ids:row.godFid}
          }).then(response => {
            this.loading = false
            if (response.status === '00000') {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();  
            }else{
              this.msgSuccess(response.message);
            }
          }).catch(()=>{
            this.loading = false
          });
        }).catch(()=>{
          this.loading = false
        });

      }).catch(function() {});
      
    },
    submitFormNew(){
      if(this.loading){
        return
      }
      if(!this.xxgNewPop.value){
        this.msgError("请选择是否新品");
        return
      }
      this.loading = true
      requestCrm({
        url:'WxService?method=wxProUpdateXxNew',
        method: 'post',
        data: {xxgNew:this.xxgNewPop.value, ids:this.xxgNewPop.id}
      }).then(response => {
        this.loading = false
        if (response.status === '00000') {
          this.msgSuccess("新增成功");
          this.xxgNewPop.open = false;
          this.getList();  
        }else{
          this.msgSuccess(response.message);
        }
      }).catch(()=>{
        this.loading = false
      });
    }
  }
};
</script>
<style scoped>
  .row-img{
    width: 80px;
    height: 80px;
  }
  .dialog-table /deep/ .current-row td{
    background-color: #1890ff;
    color: #FFFFFF;
  }
  .dialog-table /deep/ .current-row.el-table__row--striped td{
    background-color: #1890ff !important;
    color: #FFFFFF;
  }
</style>



